package com.woyao.core;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;

import com.woyao.WoyaoooApplication;
import com.woyao.core.model.Area;
import com.woyao.core.model.AreaResponse;
import com.woyao.core.service.AreaService;
import com.woyao.core.util.ServiceFactory;

import org.afinal.simplecache.ACache;

import java.io.IOException;
import java.util.ArrayList;

import retrofit2.Call;

/**
 * Created by summerwind on 2016-05-21.
 */
public class AreaLoader {
    public AreaLoader() {
    }

    public AreaLoader(Context context, String parentId) {
        this.context = context;
        this.parentId = parentId;
    }

    private Context context;

    private OnSuccessListener listener;

    public void setOnSuccessListener(OnSuccessListener listener){
        this.listener = listener;
    }

    private String parentId = "0";

    private ACache cache;
    public void load(){
        cache = ACache.get(context);
        final ArrayList<Area> childrenAreas = (ArrayList<Area>) cache.getAsObject(CacheKey.CHILDREN_AREA + parentId);
        if(childrenAreas == null){
            AsyncTask<Void,Void,AreaResponse> task =
                    new AsyncTask<Void, Void, AreaResponse>() {
                        @Override
                        protected AreaResponse doInBackground(Void... params) {
                            AreaService svc = ServiceFactory.get(AreaService.class);
                            Call<AreaResponse> areaResponseCall = svc.getArea(parentId, WoyaoooApplication.userId);
                            try {
                                AreaResponse response = areaResponseCall.execute().body();
                                return response;
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            return null;
                        }
                        @Override
                        protected void onPostExecute(final AreaResponse response) {
                            if (AreaLoader.this.listener != null) {
                                if(response != null&&response.isSuccess()&&response.getContent() != null){
                                    cache.put(CacheKey.CHILDREN_AREA + parentId, response.getContent(), ACache.TIME_DAY);
                                }
                                listener.onSuccess(response);
                            }
                        }

                        @Override
                        protected void onCancelled() {
                        }
                    };
            task.execute();
        }else{
            if(AreaLoader.this.listener != null){
                AreaResponse response = new AreaResponse();
                response.setContent(childrenAreas);
                response.setStatus("success");
                response.setMessage("");
                listener.onSuccess(response);
            }
        }

    }

    public interface OnSuccessListener {
        /**
         * Called when a view has been clicked.
         *
         * @param response The view that was clicked.
         */
        void onSuccess(AreaResponse response);
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Activity context) {
        this.context = context;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }
}
